package com.lanju.dao;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.lanju.entity.Topic;

@Repository
public class TopicDao {
	
    private static final Log log = LogFactory.getLog(ServiceDao.class);
    @Autowired
   	SessionFactory sessionFactory;
    
    @PersistenceContext(unitName = "financialmarketingUnit")
    private EntityManager entityManager;
    
    public void saveTopic(Topic entity){
    	try {
    		entityManager.persist(entity);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    public void updateTopic (Topic entity){
       	try {
    		this.sessionFactory.getCurrentSession().update(entity);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    public void delTopic (Topic entity){
       	try {
    		this.sessionFactory.getCurrentSession().delete(entity);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    public Boolean delTopic1 (Integer id){
       	try {
    		StringBuffer sql = new StringBuffer();
    		sql.append("delete from Topic where topic_id="+id);
    		entityManager.createQuery(sql.toString()).executeUpdate();
    		return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
    }

    public Topic getTopic (Integer id){
       	try {
    		return entityManager.find(Topic.class, id);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
    }
    
    public List<Topic> getTopicList (Map<String,Object> map){
       	try {
			int offset = Integer.parseInt( map.get("offset").toString() );
			int limit = Integer.parseInt( map.get("limit").toString() );
       		StringBuffer sql = new StringBuffer();
    		sql.append(" from Topic where 1=1");
    		if(!StringUtils.isEmpty(map.get("topic_name")))
    			sql.append(" and topic_name like '%"+map.get("topic_name").toString()+"%'");
    		if(!StringUtils.isEmpty(map.get("status")))
    			sql.append(" and status ='"+map.get("status").toString()+"'");
    		sql.append(" order by create_time desc");
    		Query query = entityManager.createQuery(sql.toString());
    		query.setFirstResult(offset).setMaxResults(limit);
    		return query.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
    }
    
    public Long getTopicListCount (Map<String,Object> map){
       	try {
			int offset = Integer.parseInt( map.get("offset").toString() );
			int limit = Integer.parseInt( map.get("limit").toString() );
       		StringBuffer sql = new StringBuffer();
    		sql.append("select count(1) from Topic  where 1=1");
    		if(!StringUtils.isEmpty(map.get("topic_name")))
    			sql.append(" and topic_name like '%"+map.get("topic_name").toString()+"%'");
    		if(!StringUtils.isEmpty(map.get("status")))
    			sql.append(" and status ='"+map.get("status").toString()+"'");
    		Query query = entityManager.createQuery(sql.toString());
    		return (Long) query.getSingleResult();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
    }
    
}
